import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: leetcode.简单题.两个数组的交集
 */

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int len = nums1.length > nums2.length ? nums2.length : nums1.length;
        int[] array = new int[len];
        int j = 0;
        int count = 0;
        for (int i = 0; i < nums1.length; i++) {
            while(j < nums2.length && nums2[j] < nums1[i]){
                j++;
            }
            if(nums1[i] == nums2[j] && (count == 0 || array[count - 1] != nums1[i])){
                array[count] = nums1[i];
                count++;
            }
        }
        int[] ret = Arrays.copyOf(array,count);
        return ret;
    }
}

public class Test {
    Solution solution = new Solution();
}
